﻿package plugins.mstandio.Trace {
	
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.BlendMode;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;	
	import zephyr.BroadcastEvent;	
	
	/**
	 * This is simple module that enables printing special called trace inside PanoSalado window	 
	 * @author mstandio
	 */
	public class Trace extends Sprite {
						
		static private var txt:TextField;
		static private var txtFormat:TextFormat;
		private static var buffer:String = "";
		
		private var scrollUp:Sprite;
		private var scrollDown:Sprite;
		
		private var txtWidth:int; 
		private var txtHeight:int;
		private var verticalAlign:String;				
		private var horizontalAlign:String;
		private var margin:Array;			
		private var globalAlpha:Number;
		
		private var moduleLoader:Object;
		private var ModuleLoader:Class;			
		private var BroadcastEvent:Class;				
		
		public function Trace(){
			addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);	
		}		
		
		private function stageReady(e:Event):void {                                            
			removeEventListener(Event.ADDED_TO_STAGE, stageReady);	
			
			ModuleLoader = ApplicationDomain.currentDomain.getDefinition("ModuleLoader") as Class;
            moduleLoader = ModuleLoader(parent); 
			BroadcastEvent = ApplicationDomain.currentDomain.getDefinition("zephyr.BroadcastEvent") as Class;						
			
			this.getSettings();
			this.buildAll();						
			this.handleStageResize();
			
			this.stage.addEventListener(Event.RESIZE, this.handleStageResize); 
		}				
		
		private function getSettings():void {
			var settings:XML = moduleLoader.xmlByName["Trace"];			
			
			this.txtWidth   = (settings.@width != undefined) ?  int(settings.@width) : 300;	
			this.txtHeight  = (settings.@height != undefined) ?  int(settings.@height) : 200;				
			this.globalAlpha = (settings.@alpha != undefined) ?  Number(settings.@alpha) : 0.7 ;			
			this.verticalAlign = (settings.@verticalAlign != undefined) ?  settings.@verticalAlign : "top" ;
			this.horizontalAlign = (settings.@horizontalAlign != undefined) ?  settings.@horizontalAlign : "left" ;
			this.margin = (settings.@margin != undefined) ? String(settings.@margin).split(",")  : new Array(20, 0, 0, 20) ;			
		}		
		
		private function buildAll():void {
			txtFormat = new TextFormat();
			txtFormat.blockIndent = 0;
			txtFormat.font = "Courier"
			txtFormat.color = 0xffffff;
			txtFormat.rightMargin = 20;
			
			txt = new TextField();						
			txt.width = this.txtWidth;
			txt.height = this.txtHeight;			
			txt.defaultTextFormat=txtFormat;			
			txt.background = true;
			txt.backgroundColor = 0x000000;
			txt.alpha = this.globalAlpha;						
			txt.selectable = false;
			txt.mouseEnabled = false;
			txt.border = false;
			txt.blendMode = BlendMode.LAYER;			
			txt.wordWrap = true;
			txt.multiline = true;
			txt.text = buffer;
			buffer = "";
			this.addChild(txt);			
			
			this.scrollUp = new Sprite();
			this.scrollUp.graphics.beginFill(0x000000);
			this.scrollUp.graphics.drawRect(0, 0, 20, 20);
			this.scrollUp.graphics.endFill();			
			this.scrollUp.graphics.beginFill(0xffffff);
			this.scrollUp.graphics.moveTo(this.scrollUp.width / 2, this.scrollUp.height / 3);
			this.scrollUp.graphics.lineTo(this.scrollUp.width * 0.66, this.scrollUp.height * 0.66);
			this.scrollUp.graphics.lineTo(this.scrollUp.width / 3, this.scrollUp.height * 0.66 );
			this.scrollUp.graphics.endFill();				
			this.scrollUp.alpha = this.globalAlpha;
			this.scrollUp.addEventListener(MouseEvent.CLICK, this.scrollTxtUp)			
			this.scrollUp.buttonMode = true;
			this.scrollUp.useHandCursor = true;			
			this.addChild(this.scrollUp);					
			
			this.scrollDown = new Sprite();
			this.scrollDown.graphics.beginFill(0x000000);
			this.scrollDown.graphics.drawRect(0, 0, 20, 20);
			this.scrollDown.graphics.endFill();			
			this.scrollDown.graphics.beginFill(0xffffff);
			this.scrollDown.graphics.moveTo(this.scrollDown.width / 3, this.scrollDown.height / 3);
			this.scrollDown.graphics.lineTo(this.scrollDown.width * 0.66, this.scrollDown.height / 3);
			this.scrollDown.graphics.lineTo(this.scrollDown.width / 2, this.scrollDown.height * 0.66 );
			this.scrollDown.graphics.endFill();			
			this.scrollDown.alpha = this.globalAlpha;
			this.scrollDown.addEventListener(MouseEvent.CLICK, this.scrollTxtDown);		
			this.scrollDown.buttonMode = true;
			this.scrollDown.useHandCursor = true;
			this.addChild(this.scrollDown);									
		}		
		
		
		private function handleStageResize(e:Event = null):void {								
			var resultX:Number =0;
			var resultY:Number =0;
			
			switch (this.horizontalAlign) {
				case "left": {
					resultX = 0;	
				}break;
				case "right": {
					resultX = this.stage.stageWidth - this.txtWidth;
				}break;				
				default: { // "center"
					resultX = this.stage.stageWidth * 0.5 - this.txtWidth* 0.5;
				}
			}			
			
			resultX -= Number(this.margin[1])// move right
			resultX += Number(this.margin[3])// move left
			
			switch (this.verticalAlign) {
				case "top": {
					resultY = 0;
				}break;
				case "bottom": {
					resultY = this.stage.stageHeight - this.txtHeight;
				}break;				
				default: { // "center"
					resultY = this.stage.stageHeight * 0.5 - this.txtHeight * 0.5;
				}
			}
			
			resultY += Number(this.margin[0])// move up
			resultY -= Number(this.margin[2])// move down
						
			txt.x = resultX;
			txt.y = resultY;		
			
			this.scrollUp.x = txt.x+txt.width-this.scrollUp.width;
			this.scrollUp.y = txt.y;
			this.scrollDown.x = txt.x+txt.width-this.scrollDown.width;
			this.scrollDown.y = txt.y + txt.height-this.scrollDown.height;
		}		
		
		private function scrollTxtUp(e:Event):void {
			txt.scrollV -= 1;
		}
		
		private function scrollTxtDown(e:Event):void {
			txt.scrollV += 1;
		} 		
		
		public static function add(string:String):void {
			if (txt == null){					
				buffer += string + "\n"; 								
			}else {								
				txt.appendText(string + "\n");				
				txt.scrollV = txt.maxScrollV;							
			}			
		}				
		
		public static function clear():void {			
			if (txt == null){					
				buffer = "";
			}else {
				txt.text = "";
			}						
		}		
	}
}